package com.moodincode.taskcenter;

import com.moodincode.taskcenter.bean.JobProxy;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;


@Component
public class TestScheduler extends QuartzJobBean {
    @Autowired
    private SchedulerFactoryBean schedulerFactoryBean;
    @Autowired
    private ITestService testService;

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        System.out.println("test scheduler:this is running ");
        testService.hello();
    }

    /* public void test() {
            System.out.println("test scheduler:this is running ");

        }*/
    //@PostConstruct
    public void init() throws SchedulerException {
        Scheduler scheduler = schedulerFactoryBean.getScheduler();

        JobDetail job = JobBuilder.newJob(Test2.class)
                .withIdentity("job1", "group1")
                .build();

        //
        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("trigger1", "group1")
                .startNow()
                .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                        .withIntervalInSeconds(4)
                        .repeatForever())
                .build();

        // Tell quartz to schedule the job using our trigger
        scheduler.scheduleJob(job, trigger);


        JobDetail job2 = JobBuilder.newJob(JobProxy.class)
                .withIdentity("job2", "group1")
                .build();
        job2.getJobDataMap().put(JobProxy.DATA_JOB_BEAN_CLASS_NAME,"com.moodincode.taskcenter.Test3");
        job2.getJobDataMap().put(JobProxy.DATA_JOB_METHOD_NAME,"hello");
        //
        Trigger trigger2 = TriggerBuilder.newTrigger()
                .withIdentity("trigger2", "group1")
                .startNow()
                .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                        .withIntervalInSeconds(4)
                        .repeatForever())
                .build();
        trigger2.getJobDataMap().put(JobProxy.DATA_JOB_BEAN_CLASS_NAME,"com.moodincode.taskcenter.Test3");
        trigger2.getJobDataMap().put(JobProxy.DATA_JOB_METHOD_NAME,"hello");
        // Tell quartz to schedule the job using our trigger
        scheduler.scheduleJob(job2, trigger2);

        System.out.println("==============="+scheduler.isStarted());
    }

}
